package com.shine.app.ui;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shine.app.R;
import com.shine.app.adapter.ViewPagerAdapter;
import com.shine.app.bean.PlaceNearBean;
import com.shine.app.bean.SearchBean;
import com.shine.app.commons.Commons;
import com.shine.app.commons.StringUtils;
import com.shine.app.commons.UIHelper;
import com.shine.app.database.DataBaseManager;

public class LocationBaseActivity extends CustomViewpagerActivity implements
		OnItemClickListener {

	protected ImageView gobackImageView;
	protected ImageView searchImageView;
	protected EditText autoCompleteTextView;
	protected TextView headNameTextView;

	protected ListView nearListView;
	protected ListView recentListView;
	protected ListView popularListView;
	protected ProgressBar progressBar_recent;
	protected ProgressBar progressBar_near;
	protected ProgressBar progressBar_popular;

	protected LinearLayout searchLayout;
	protected SearchBean searchBean;
	protected DataBaseManager dataBaseManager = new DataBaseManager(this);

	protected void init() {
		initHeadView();
		recentListView = (ListView) pageTwo.findViewById(R.id.listview);
		popularListView = (ListView) pageThree.findViewById(R.id.listview);
		nearListView = (ListView) pageOne.findViewById(R.id.listview);
		progressBar_near = (ProgressBar) pageOne.findViewById(R.id.progressBar);
		progressBar_recent = (ProgressBar) pageTwo
				.findViewById(R.id.progressBar);
		progressBar_popular = (ProgressBar) pageThree
				.findViewById(R.id.progressBar);
		progressBar_popular.setVisibility(View.GONE);
	}

	protected void initHeadView() {
		searchBean = (SearchBean) getIntent()
				.getSerializableExtra("searchBean");
		gobackImageView = (ImageView) this.findViewById(R.id.head_back);
		searchImageView = (ImageView) this.findViewById(R.id.head_seacher);
		autoCompleteTextView = (EditText) this.findViewById(R.id.searche_keys);
		headNameTextView = (TextView) this.findViewById(R.id.head_name);
	}

	protected void InitViewPager() {
		pagerList = new ArrayList<View>();
		pageOne = inflater.inflate(R.layout.location_popular, null);
		pageTwo = inflater.inflate(R.layout.location_popular, null);
		pageThree = inflater.inflate(R.layout.location_popular, null);
		pagerList.add(pageOne);
		pagerList.add(pageTwo);
		pagerList.add(pageThree);
		viewPager.setAdapter(new ViewPagerAdapter(pagerList));
		viewPager.setCurrentItem(0);
	}

	@Override
	protected void onResume() {
		super.onResume();
		initRecentsCount();
	}

	protected void initRecentsCount() {
		String message = this.getResources().getString(
				R.string.location_recents);
		int length = 0;
		List<PlaceNearBean> list = dataBaseManager.find();
		length = list.size();
		textView2.setText(message + " (" + length + ")");
	}

	protected void clickListener() {
		nearListView.setOnItemClickListener(this);
		recentListView.setOnItemClickListener(this);
		popularListView.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		TextView idTextView = (TextView) view.findViewById(R.id.id);
		String ids = idTextView.getText().toString();
		if (StringUtils.isEmpty(ids)) {
			return;
		}
		PlaceNearBean placeNearBean = (PlaceNearBean) idTextView.getTag();
		if (placeNearBean == null
				|| "".equals(placeNearBean.getLocation().getName())) {
			return;
		}
		if (searchBean == null) {
			searchBean = new SearchBean();
		}
		searchBean.setPlaceNearBean(placeNearBean);
		appContext.saveObject(searchBean, Commons.SEARCH_KEY);
		String name = placeNearBean.getLocation().getFullName();
		boolean flag = dataBaseManager.findByName(name);
		if (flag) {
			dataBaseManager.delete(name);
		}
		dataBaseManager.add(placeNearBean);
		UIHelper.showHome(LocationBaseActivity.this);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
	}

}
